<?php
namespace app\customer\model;

use app\BaseModel;

class LongbingCardQuestionnaire extends BaseModel
{
    //定义表名称
    protected $name = 'longbing_card_qn_questionnaire';
    
    public function searchTitletAttr($query, $value, $data)
    {
        $query->where('content','like', '%' . $value . '%');
    }
    public function searchUniacidAttr($query, $value, $data)
    {
        $query->where('uniacid','=',  $value );
    }
    
    public function question()
    {
        return $this->hasMany('LongbingCardQuestion' ,'naire_id' ,'id');
    }
    //创建
    public function createQuestionnaire($data){
        $data['create_time'] = time();
        $result = $this->save($data);
        if(!empty($result)) $result = $this->id;
        return $result;
    }
    
    //修改
    public function updateQuestionnaire($filter ,$data)
    {
        $data['update_time'] = time();
        $result = $this->where($filter)->update($data);
        return !empty($result);
    }
    //获取列表
    public function listQuestionnaire($filter ,$page_config)
    {
        $result = $this->withSearch(['title' ,'uniacid'] ,$filter)
                       ->order('id' ,'desc')
                       ->page($page_config['page'] ,$page_config['page_count'])
                       ->select();
        if(!empty($result)) $result = $result->toArray(); 
        return $result;
    }
    //获取总数
    public function getQuestionnaireCount($filter)
    {
        $result = $this->withSearch(['title' ,'uniacid'] ,$filter)->count();
        if(empty($result)) $result = 0;
        return $result;
    }

    //获取详情
    public function getQuestionnaire($filter)
    {
        $result = $this->with(['question'])
                       ->where($filter)
                       ->find();
        if(!empty($result)) $result = $result->toArray(); 
        return $result;
    }
    //删除
    public function delQuestionnaire($filter)
    {
        $result = $this->where($filter)->delete();
        return !empty($result);
    }
}